package com.fireline.beauty.cashier;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.member.HyglEdit;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.FunctionDialog;
import com.zgw.fireline.base.frame.IFunction;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

/**
 * 前台收银 主页面
 * */
public class CashierHomeView implements IFunction {

	private Shell shell;
	private Text text;
	private CLabel lblNewLabel_2;
	private CLabel label_3;
	private CLabel label_5;
	private CLabel label_7;
	private CLabel label_9;
	private CLabel label_11;
	private CLabel label_13;
	private CLabel label_15;
	private TabFolder tabFolder;

	private HyxfView hyxfView;
	private HyjfView hyjfView;
	private YebdView yebdView;
	private JfglView jfglView;
	private BztxEdit bztxView;
	private HyxxTextPopup popupHyxx;
	private Dataset hyxx;// 会员信息

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		this.shell = parent.getShell();
		Composite content = new Composite(parent, SWT.NONE);
		GridLayout gl_content = new GridLayout(1, false);
		gl_content.marginWidth = 0;
		content.setLayout(gl_content);

		Composite composite = new Composite(content, SWT.NONE);
		GridLayout gl_composite = new GridLayout(3, false);
		gl_composite.marginWidth = 40;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel.setText("请在此输入产品编号或名称：");

		text = new Text(composite, SWT.BORDER);
		text.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				popupHyxx.show("");
			}
		});
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1);
		gd_text.widthHint = 130;
		text.setLayoutData(gd_text);
		// 选中一个会员
		popupHyxx = new HyxxTextPopup(text, new Listener() {
			@Override
			public void handleEvent(Event event) {
				Dataset hyxxSet = (Dataset) event.data;
				doSelectedhy(hyxxSet);
			}
		});

		ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.RIGHT);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.setImage(SWTResourceManager.getImage(CashierHomeView.class,
				"/com/fireline/beauty/image/query.png"));
		tltmNewItem.setText("查询");
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				popupHyxx.show("");
			}
		});

		ToolItem toolItem_3 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.setText("散客");
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSelectedsk();
			}
		});
		ToolItem toolItem_4 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem toolItem = new ToolItem(toolBar, SWT.NONE);
		toolItem.setText("添加会员");
		toolItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doAddhy();
			}
		});

		ToolItem toolItem_5 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem toolItem_1 = new ToolItem(toolBar, SWT.NONE);
		toolItem_1.setText("修改会员");
		toolItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doEdithy();
			}
		});

		ToolItem toolItem_6 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem toolItem_2 = new ToolItem(toolBar, SWT.NONE);
		toolItem_2.setText("清除会员");
		toolItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doClearHy();
			}
		});

		Label label = new Label(content, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Composite composite_1 = new Composite(content, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(8, false);
		gl_composite_1.marginWidth = 40;
		gl_composite_1.horizontalSpacing = 0;
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Label lblNewLabel_1 = new Label(composite_1, SWT.NONE);
		lblNewLabel_1.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));
		lblNewLabel_1.setText("会员编号：");

		lblNewLabel_2 = new CLabel(composite_1, SWT.NONE);
		GridData gd_lblNewLabel_2 = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_lblNewLabel_2.widthHint = 100;
		lblNewLabel_2.setLayoutData(gd_lblNewLabel_2);
		lblNewLabel_2
				.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		lblNewLabel_2.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		Label label_2 = new Label(composite_1, SWT.NONE);
		GridData gd_label_2 = new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1);
		gd_label_2.horizontalIndent = 10;
		label_2.setLayoutData(gd_label_2);
		label_2.setText("会员姓名：");
		label_2.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		label_3 = new CLabel(composite_1, SWT.NONE);
		GridData gd_label_3 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_3.widthHint = 100;
		label_3.setLayoutData(gd_label_3);
		label_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_3.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		Label label_4 = new Label(composite_1, SWT.NONE);
		GridData gd_label_4 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_4.horizontalIndent = 10;
		label_4.setLayoutData(gd_label_4);
		label_4.setText("会员卡名：");
		label_4.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		label_5 = new CLabel(composite_1, SWT.NONE);
		GridData gd_label_5 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_5.widthHint = 100;
		label_5.setLayoutData(gd_label_5);
		label_5.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_5.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		Label label_6 = new Label(composite_1, SWT.NONE);
		GridData gd_label_6 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_6.horizontalIndent = 10;
		label_6.setLayoutData(gd_label_6);
		label_6.setText("联系电话：");
		label_6.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		label_7 = new CLabel(composite_1, SWT.NONE);
		GridData gd_label_7 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 1);
		gd_label_7.widthHint = 120;
		label_7.setLayoutData(gd_label_7);
		label_7.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_7.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		Label label_8 = new Label(composite_1, SWT.NONE);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_8.setText("会员生日：");
		label_8.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		label_9 = new CLabel(composite_1, SWT.NONE);
		label_9.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		label_9.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_9.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		Label label_10 = new Label(composite_1, SWT.NONE);
		label_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_10.setText("累计交费：");
		label_10.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		label_11 = new CLabel(composite_1, SWT.NONE);
		label_11.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				tabFolder.setSelection(1);
				refreshHyxx();
			}
		});
		label_11.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		label_11.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		label_11.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_11.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD, false,
				true));

		Label label_12 = new Label(composite_1, SWT.NONE);
		label_12.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_12.setText("卡内佘额：");
		label_12.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));

		label_13 = new CLabel(composite_1, SWT.NONE);
		label_13.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				tabFolder.setSelection(3);
				refreshHyxx();
			}
		});
		label_13.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		label_13.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_13.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD, false,
				true));

		Label label_14 = new Label(composite_1, SWT.NONE);
		label_14.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_14.setText("会员积分：");
		label_14.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD));
		label_15 = new CLabel(composite_1, SWT.NONE);
		label_15.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				tabFolder.setSelection(2);
				refreshHyxx();
			}
		});
		label_15.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		label_15.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));
		label_15.setFont(SWTResourceManager.getFont("宋体", 11, SWT.BOLD, false,
				true));

		Label label_1 = new Label(content, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		tabFolder = new TabFolder(content, SWT.NONE);
		tabFolder.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NORMAL));
		GridData gd_tabFolder = new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1);
		gd_tabFolder.horizontalIndent = 5;
		tabFolder.setLayoutData(gd_tabFolder);

		TabItem tbtmNewItem = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem.setText("会员消费");
		hyxfView = new HyxfView();
		tbtmNewItem.setControl(hyxfView.createControl(tabFolder));
		tbtmNewItem.setData(hyxfView);

		TabItem tbtmNewItem_1 = new TabItem(tabFolder, SWT.NONE);
		tbtmNewItem_1.setText("会员交费");
		hyjfView = new HyjfView();
		tbtmNewItem_1.setControl(hyjfView.createControl(tabFolder));
		tbtmNewItem_1.setData(hyjfView);
		hyjfView.setChangeListener(new Listener() {
			@Override
			public void handleEvent(Event event) {
				if (hyxx != null) {
					String hybh = hyxx.getString("hybh");
					popupHyxx.refreshData();
					doSelectedhy(popupHyxx.getHyxx(hybh));
				}
			}
		});
		TabItem tabItem = new TabItem(tabFolder, 0);
		tabItem.setText("积分管理");
		jfglView = new JfglView();
		tabItem.setControl(jfglView.createControl(tabFolder));
		tabItem.setData(jfglView);

		TabItem tabItem_1 = new TabItem(tabFolder, 0);
		tabItem_1.setText("佘额变动");
		yebdView = new YebdView();
		tabItem_1.setControl(yebdView.createControl(tabFolder));
		tabItem_1.setData(yebdView);
		TabItem tabItem_2 = new TabItem(tabFolder, 0);
		tabItem_2.setText("备注提醒");
		bztxView = new BztxEdit();
		tabItem_2.setControl(bztxView.createControl(tabFolder));
		tabItem_2.setData(bztxView);
		initializeListener();
		setParams();
		return content;
	}

	/*
	 * 清除界面上的会员信息
	 */
	protected void doClearHy() {
		if (hyxx != null && hyxx.getSelectedIndex() != -1) {
			hyxx.setSelected(-1);
			doSelectedhy(hyxx);
		}
	}

	/*
	 * 修改当前选中会员信息
	 */
	protected void doEdithy() {
		if (hyxx == null && hyxx.getSelectedIndex() == -1) {
			MessageDialog.openInformation(shell, "提示", "未选择会员");
			return;
		}
		String hybh = hyxx.getString("hybh");
		if (hybh.equals("00000")) {
			MessageDialog.openInformation(shell, "提示", "不能修改散客信息");
			return;
		}
		HyglEdit hygl = new HyglEdit(hybh);
		FunctionDialog dlg = new FunctionDialog(shell, hygl);
		dlg.open();
		if (hygl.result == SWT.OK) {
			popupHyxx.refreshData();
			doSelectedhy(popupHyxx.getHyxx(hybh));
		}
	}

	/*
	 * 选择散客 散客编号:00000
	 */
	protected void doSelectedsk() {
		doSelectedhy(popupHyxx.getHyxx("00000"));
	}

	/*
	 * 添加会员
	 */
	protected void doAddhy() {
		HyglEdit hygl = new HyglEdit();
		FunctionDialog dlg = new FunctionDialog(shell, hygl);
		dlg.open();
		if (hygl.result == SWT.OK) {
			popupHyxx.refreshData();
			doSelectedhy(popupHyxx.getHyxx(hygl.hybh));
		}
	}

	private void initializeListener() {
		tabFolder.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (hyxx != null)
					refreshHyxx();
			}
		});
	}

	/**
	 * <b> 选中一个会员,并显示会员基本信息 </b>
	 * */
	public void doSelectedhy(Dataset hyxxSet) {
		lblNewLabel_2.setText(hyxxSet.getValue("hybh", String.class, ""));
		label_3.setText(hyxxSet.getValue("hyxm", String.class, ""));
		label_5.setText(hyxxSet.getValue("hykmc", String.class, ""));
		label_7.setText(hyxxSet.getValue("lxdh", String.class, ""));
		label_9.setText(hyxxSet.getStringForExpr("${sr}${csrq}"));
		label_11.setText(hyxxSet.getStringForExpr("${hyjf}元"));
		label_13.setText(hyxxSet.getStringForExpr("${knje}元"));
		label_15.setText(hyxxSet.getStringForExpr("${jy}分"));
		text.setText("");
		label_15.getParent().layout();
		this.hyxx = hyxxSet;
		refreshHyxx();
	}

	/*
	 * 刷新当前选中会员 消费、交费、佘额变动、备注信息
	 */
	private void refreshHyxx() {
		String hybh = hyxx.getString("hybh");
		Object data = tabFolder.getSelection()[0].getData();
		if (data == hyxfView) {
			hyxfView.refresh(hybh);
		} else if (data == hyjfView) {
			hyjfView.refresh(hybh);
		} else if (data == jfglView) {
			jfglView.refresh(hybh);
		} else if (data == yebdView) {
			yebdView.refresh(hybh);
		} else if (data == bztxView) {
			bztxView.refresh(hybh);
		}
	}

	protected void setParams() {
	}

	public static void main(String[] args) {
		IFunction home = new CashierHomeView();
		FunctionDialog dlg = new FunctionDialog(new Shell(), home);
		dlg.open();
	}

}
